/**
 * com.baowei.wwb.common.page.PageUtil
 * author:chenw
 * create: 15/7/27.16:20
 * email:javacspring@hotmail.com
 */

package cn.yx.framework.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: chenw
 * @time: 15/7/27 16:20
 */
public class PageUtil {

    public static Map<String, Object> make(long total, int pageIndex, int pageSize, List data) {
        long totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        return new HashMap() {{
            put("total", total);
            put("next", pageIndex < totalPage);
            put("prev", pageIndex > 1);
            put("list", data);
            put("index", pageIndex);
        }};
    }

    // pageIndex 从1开始
    public static List getPageList(int pageSize, int pageIndex, List list) {
        List<Object> returnList = null;
        if(list == null || list.size() == 0) {
            return null;
        } else {
            int totalSize = list.size();
            int fromIndex = (pageIndex-1) * pageSize;
            int endIndex = fromIndex + pageSize;
            if(totalSize < endIndex) {
                endIndex = totalSize;
            }
            if(totalSize < fromIndex) {
                return null;
            } else {
                returnList = new ArrayList<Object>();
                for(int i=fromIndex; i<endIndex; i++) {
                    returnList.add(list.get(i));
                }
            }
        }

        return returnList;
    }
}
